/*
 * FileDateModification.cpp
 *
 *
 * */

#include "FileDateModification.h"

FileDateModification::FileDateModification() {
	this->clock = new struct tm;
	fromFile = true;
}

FileDateModification::FileDateModification(const char* filename) {
	this->clock = new struct tm;
	this->createFromFile(filename);
	fromFile = true;
}

FileDateModification::FileDateModification(const FileDateModification& fileDateMod) {
	memcpy(this->clock, fileDateMod.clock, sizeof(struct tm));

	this->date = fileDateMod.date;

	this->fromFile = false;
}

FileDateModification::~FileDateModification() {
	if (this->clock) {
		delete this->clock;
		this->clock = NULL;
	}
}

void FileDateModification::createFileModDate(const char* filename) {
	this->createFromFile(filename);
}

void FileDateModification::createFromFile(const char* filename) {
	struct stat attrib;
	stat(filename, &attrib);	// Obtengo atributos del archivo
	struct tm* aux = localtime(&(attrib.st_mtime));	// Obtengo fecha ultima modificacion
	memcpy(this->clock, aux, sizeof(struct tm));
	this->createStringDate();
}

void FileDateModification::createStringDate() {
	std::stringstream stream;
	char barra = '/', guion = '-', dosPuntos = ':';

	// Estructura de la fecha: dia/mes/anio-hora:min:sec
	stream << this->clock->tm_mday;
	stream.put(barra);

	this->clock->tm_mon += 1;
	stream << this->clock->tm_mon;
	stream.put(barra);

	this->clock->tm_year += 1900;
	stream << this->clock->tm_year;
	stream.put(guion);

	stream << this->clock->tm_hour;
	stream.put(dosPuntos);

	stream << this->clock->tm_min;
	stream.put(dosPuntos);

	stream << this->clock->tm_sec;

	this->date = stream.str();
}

//	Estructura de string, fecha de modificacion: dia/mes/anio-hora:min:sec
std::string FileDateModification::getFileDateMod() {
	return this->date;
}

void FileDateModification::operator=(const FileDateModification& fDate) {
	memcpy(this->clock, fDate.clock, sizeof(struct tm));
	this->date = fDate.date;
	this->fromFile = fDate.fromFile;
}

std::ostream& operator<<(std::ostream &o, const FileDateModification &fDate) {
	o << fDate.date;
	return o;
}

std::istream& operator>>(std::istream &i, FileDateModification &fDate) {
	std::stringstream stream;
	i >> fDate.date;
	char aux;

	stream << fDate.date;

	stream >> fDate.clock->tm_mday;
	stream >> aux;	// '/'
	stream >> fDate.clock->tm_mon;
	stream >> aux;	// '/'
	stream >> fDate.clock->tm_year;
	stream >> aux;	// '-'
	stream >> fDate.clock->tm_hour;
	stream >> aux;	// ':'
	stream >> fDate.clock->tm_min;
	stream >> aux;	// ':'
	stream >> fDate.clock->tm_sec;

	fDate.fromFile = false;
	return i;
}

int FileDateModification::compare(const FileDateModification& fMod) {
	if (this->clock->tm_year < fMod.clock->tm_year) {
		return -1;
	} else if (this->clock->tm_year > fMod.clock->tm_year) {
		return 1;
	} else {
		// mismo anio
		if (this->clock->tm_mon < fMod.clock->tm_mon) {
			return -1;
		} else if (this->clock->tm_mon > fMod.clock->tm_mon) {
			return 1;
		} else {
			// mismo mes
			if (this->clock->tm_mday < fMod.clock->tm_mday) {
				return -1;
			} else if (this->clock->tm_mday > fMod.clock->tm_mday) {
				return 1;
			} else {
				// mismo dia
				if (this->clock->tm_hour < fMod.clock->tm_hour) {
					return -1;
				} else if (this->clock->tm_hour > fMod.clock->tm_hour) {
					return 1;
				} else {
					// mismo hora
					if (this->clock->tm_min < fMod.clock->tm_min) {
						return -1;
					} else if (this->clock->tm_min > fMod.clock->tm_min) {
						return 1;
					} else {
						// mismo minuto
						if (this->clock->tm_sec < fMod.clock->tm_sec) {
							return -1;
						} else if (this->clock->tm_sec > fMod.clock->tm_sec) {
							return 1;
						} else {
							// fechas iguales
							return 0;
						}
					}
				}
			}
		}
	}
}
